﻿using System.Xml;

namespace EasyDLL.Data
{
    public class XmlWrapper
    {
        #region  增

        /// <summary>
        /// 新建一个带有根节点的xml
        /// </summary>
        /// <param name="rootName">根节点名字</param>
        /// <returns></returns>
        public static XmlDocument CreateXml(string rootName)
        {
            XmlDocument xml = new XmlDocument();

            //写入声明
            XmlNode node = xml.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xml.AppendChild(node);

            //增加根节点
            XmlElement rootElement = xml.CreateElement(rootName);
            xml.AppendChild(rootElement);

            return xml;
        }

        /// <summary>
        /// 添加一个父节点到根节点末尾
        /// </summary>
        /// <param name="xml">xml文档</param>
        /// <param name="parentNodeName">父节点名字</param>
        /// <returns></returns>
        public static void AddParentNode(XmlDocument xml, string parentNodeName)
        {
            XmlElement parentElement = xml.CreateElement(parentNodeName);
            if (xml.DocumentElement != null)
            {
                xml.DocumentElement.AppendChild(parentElement);
            }
        }


        /// <summary>
        /// 添加一个新的子节点(包括属性信息)到指定的节点中
        /// </summary>
        /// <param name="xml">xml文档</param>
        /// <param name="currentNodePath"></param>
        /// <param name="elementName">新节点的名字</param>
        /// <param name="attrib">新节点的属性名字</param>
        /// <param name="attribContent">新节点的属性值</param>
        /// <param name="content">新节点值</param>
        /// <returns></returns>
        public static bool AddElement(XmlDocument xml, string currentNodePath, string elementName
                                             , string attrib, string attribContent, string content)
        {
            bool flag = false;
            XmlNode currentNode = xml.SelectSingleNode(currentNodePath);
            if (currentNode != null)
            {
                XmlElement newElement = xml.CreateElement(elementName);
                newElement.SetAttribute(attrib, attribContent);
                newElement.InnerText = content;
                currentNode.AppendChild(newElement);
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 添加一个新的子节点(不包括属性信息)到指定的节点中
        /// </summary>
        /// <param name="xml">xml文档</param>
        /// <param name="currentNodePath">新节点的父节点xPath路径</param>
        /// <param name="elementName">新节点名字</param>
        /// <param name="content">新节点值</param>
        /// <returns></returns>
        public static bool AddElement(XmlDocument xml, string currentNodePath, string elementName
                                            , string content)
        {
            bool flag = false;
            XmlNode currentNode = xml.SelectSingleNode(currentNodePath);
            if (currentNode != null)
            {
                XmlElement newElement = xml.CreateElement(elementName);
                newElement.InnerText = content;
                currentNode.AppendChild(newElement);
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 向某一节点添加属性
        /// </summary>
        /// <param name="xml">xml文档</param>
        /// <param name="currentNodePath">属性的节点的xPath</param>
        /// <param name="attributeName">属性名</param>
        /// <param name="attributeValue">属性值</param>
        /// <returns></returns>
        public static bool AddAttribute(XmlDocument xml, string currentNodePath, string attributeName
                                               , string attributeValue)
        {
            bool flag = false;
            XmlNode currentNode = xml.SelectSingleNode(currentNodePath);
            if (currentNodePath != null)
            {
                XmlAttribute attribute = xml.CreateAttribute(attributeName);  //创建一个新属性
                if (currentNode != null)
                {
                    if (currentNode.Attributes != null)
                    {
                        currentNode.Attributes.Append(attribute);
                    }
                    XmlElement currentElement = currentNode as XmlElement;
                    if (currentElement != null)
                    {
                        currentElement.SetAttribute(attributeName, attributeValue);
                    }
                }
                flag = true;
            }
            return flag;
        }

        #endregion

        #region  查

        /// <summary>
        /// 查找指定节点名的节点列表
        /// </summary>
        /// <param name="xml">xml文档</param>
        /// <param name="nodeName">节点名称</param>
        /// <returns>符合要求的节点列表</returns>
        public static XmlNodeList QueryNodeList(XmlDocument xml, string nodeName)
        {
            XmlNodeList nodeList = xml.GetElementsByTagName(nodeName);
            if (nodeList.Count != 0)
            {
                return nodeList;
            }
            return null;
        }

        /// <summary>
        /// 查找指定节点的指定属性值
        /// </summary>
        /// <param name="xml">xml文档</param>
        /// <param name="nodePath">节点的xPath路径,必须是独一无二的。如果有多个则只返回第一个</param>
        /// <param name="attributeName">要查询的属性</param>
        /// <returns></returns>
        public static string QueryAttribute(XmlDocument xml, string nodePath, string attributeName)
        {
            string res = null;
            XmlNode currentNode = xml.SelectSingleNode(nodePath);
            if (currentNode != null)
            {
                XmlAttributeCollection xac = currentNode.Attributes;  //当前节点的属性集合
                for (int i = 0; i < xac.Count; i++)
                {
                    if (xac[i].Name == attributeName)
                    {
                        res = xac[i].Value;
                        break;
                    }
                }
            }
            return res;
        }

        /// <summary>
        /// 查找指定节点的值
        /// </summary>
        /// <param name="xml">xml文档</param>
        /// <param name="nodepath">节点的xPath路径,必须是独一无二的。如果有多个则只返回第一个</param>
        /// <returns></returns>
        public static string QueryNode(XmlDocument xml, string nodepath)
        {
            string res = null;
            XmlNode currentNode = xml.SelectSingleNode(nodepath);
            if (currentNode != null)
            {
                res = currentNode.InnerText;
            }
            return res;
        }

        #endregion

        #region 存

        public static void Save(XmlDocument xml, string path)
        {
            xml.Save(path);
        }

        #endregion
    }
}
